﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Model;

namespace DAL
{
    public class Config : BaseRepository<ConfigInfo>
    {
        #region 静态变量定义部分
        private const string PARM_ID = "@ID";
        private const string PARM_ITEMNAME = "@ItemName";
        private const string PARM_TYPEID = "@TypeID";
        private const string PARM_TYPENAME = "@TypeName";
        #endregion

        public override ConfigInfo LoadEntity<T1>(T1 Id)
        {
            SqlParameter param = GetParameter();
            param.Value = Id;
            return ExcuteSqlToGetEntity("tb_Config_GetbyID", param);
        }

        public override IList<ConfigInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("tb_Config_GetAll", null);
        }


        #region 重写基类部分
        public override string GetInsertsp()
        {
            return "tb_Config_Insert";
        }

        public override string GetDeletesp()
        {
            return "tb_Config_Delete";
        }

        public override ConfigInfo GetEntityRecord(SqlDataReader rdr)
        {
            ConfigInfo cinfo = new ConfigInfo
            {
                ID = rdr.GetInt32(0),
                ItemName = rdr.GetValue(1).ToString(),
                TypeID = rdr.GetInt32(2),
                TypeName = rdr.GetValue(3).ToString(),
            };
            return cinfo;
        }

        public override ConfigInfo GetEntityRecord()
        {
            ConfigInfo cinfo = new ConfigInfo();
            return cinfo;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter param = new SqlParameter(PARM_ID, SqlDbType.Int, 4);
            return param;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_ITEMNAME, SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_TYPEID, SqlDbType.Int, 4)
                };
            return parms;
        }

        public override void SetParameters(ConfigInfo Entity, params SqlParameter[] Parms)
        {
            Parms[0].Value = Entity.ItemName;
            Parms[1].Value = Entity.TypeID;
        }

        #endregion
    }



    public class ConfigType : BaseRepository<ConfigTypeInfo>
    {
        public override IList<ConfigTypeInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("tb_ConfigType_GetAll", null);
        }

        #region 重写基类部分
        public override ConfigTypeInfo GetEntityRecord(SqlDataReader rdr)
        {
            ConfigTypeInfo ci = new ConfigTypeInfo
            {
                TypeID = rdr.GetInt32(0),
                TypeName = rdr.GetValue(1).ToString(),
            };
            return ci;
        }
        #endregion
    }
}
